package com.dd.cloud.user.service.chknowl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.vo.PageReq;
import com.dd.cloud.user.entity.chknowl.ChknowlBooksPresent;
import com.dd.cloud.user.entity.chknowl.ChkonwlDoctorBook;
import com.dd.cloud.user.mapper.chknowl.ChkonwlDoctorBookMapper;
import com.dd.cloud.user.req.chknowl.ChBooksPageReq;
import com.dd.cloud.user.req.chknowl.ChknowlBookDoctorSettingReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 我的书架 服务实现类
 * </p>
 *
 * @author czg
 * @since 2023-10-17
 */
@Service
public class ChkonwlDoctorBookServiceImpl extends ServiceImpl<ChkonwlDoctorBookMapper, ChkonwlDoctorBook> implements IChkonwlDoctorBookService {

    @Override
    public IPage<ChknowlBooksPresent> queryDoctorBook(ChBooksPageReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.DOCTOR).get();
        IPage<ChknowlBooksPresent> page = req.pagePojo();
        IPage<ChknowlBooksPresent> res = this.getBaseMapper().queryDoctorBook(page, loginInfo.getId(), req.getKeyword());
        return res;
    }

    @Override
    public void addDoctorBook(Integer bookId, Integer doctorId) {
//        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.DOCTOR).get();
        ChkonwlDoctorBook doctorBook = new ChkonwlDoctorBook();
        doctorBook.setDoctorId(doctorId).setBookId(bookId).setCreateDate(LocalDateTime.now()).setStatus(1);
        Boolean flag = this.save(doctorBook);
        if (!flag) {
            throw new ZekeException("添加书籍失败");
        }
    }

    @Override
    public void addDoctorBookSetting(ChknowlBookDoctorSettingReq req) {

    }

    @Override
    public Boolean queryIsHaveBook(Integer bookId, Integer doctorId) {
//        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.DOCTOR).get();
        LambdaQueryWrapper<ChkonwlDoctorBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChkonwlDoctorBook::getDoctorId, doctorId)
                .eq(ChkonwlDoctorBook::getBookId, bookId)
                .eq(ChkonwlDoctorBook::getStatus, 1);
        Integer count = this.count(wrapper);
        if (count > 0) {
            return false;
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void chkonwlDoctorBookService(String bookIds, Integer doctorId) {
        if (StringUtils.isBlank(bookIds)) {
            throw new ZekeException("书籍id不能为空");
        }
        String[] bookIdStr = bookIds.split(",");
        List<Integer> bookIdList = new ArrayList<>();
        for (String bookId : bookIdStr) {
            bookIdList.add(Integer.parseInt(bookId));
        }
//        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.DOCTOR).get();
        LambdaUpdateWrapper<ChkonwlDoctorBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(ChkonwlDoctorBook::getBookId, bookIdList)
                .eq(ChkonwlDoctorBook::getDoctorId, doctorId)
                .set(ChkonwlDoctorBook::getStatus, -1);
        Boolean flag = this.update(updateWrapper);
        if (!flag) {
            throw new ZekeException("删除书籍失败");
        }
    }
}
